import { attribute_mana } from '../../../element/attributeMana/Classes/cla_attributeMana';
import { removeObjectOnlyPushSet } from '../../../Func/uniFunc/Func/ObjectOnlyPushSet_operate';
import { dragList_fromToSapcename_analysis } from './cla_dragList_fromToSpacename_analysis';

const dragList_dataMana_bodyTag = 'dragList_dataMana_body';

const split_dl_data = ',';

const dl_body_spacename = 'black_';

/**
 *  body 挂载 的 属性名
 * @type {{
 *  from: 'dragSpace_from',
 *  to: 'dragSpace_to',
 *  data: 'drag_dataList',
 *  spacename: 'currentSpacename',
 *  toEl: 'drag_toEl',
 *}}
 */
let DL_body_attribute = {
  from: 'dragSpace_from',
  to: 'dragSpace_to',
  data: 'drag_dataList',
  spacename: 'currentSpacename',
  toEl: 'drag_toEl',
};

class dragList_dataMana_body extends attribute_mana {
  get [Symbol.toStringTag]() {
    return dragList_dataMana_bodyTag;
  }

  constructor() {
    super(document.body);
    // 设置 body 的 dragList_sapcename = 'balck_'
    this.setAttribute(DL_body_attribute.spacename, dl_body_spacename);
  }

  /**
   * dragList 中间 元素
   * @type {HTMLElement}
   */
  dragLisT_floatDomEl;

  /**
   * 设置 toSpace
   * @param {string} newVal
   */
  set dragTo(newVal) {
    this.setAttribute(DL_body_attribute.to, newVal);
  }

  /**
   * 获取 toSpace
   */
  get dragTo() {
    return this.getAttribute(DL_body_attribute.to);
  }
  /**
   * 设置 fromSpace
   * @param {string} newVal
   */
  set dragFrom(newVal) {
    this.setAttribute(DL_body_attribute.from, newVal);
  }

  /**
   * 获取 fromSpace
   */
  get dragFrom() {
    return this.getAttribute(DL_body_attribute.from);
  }

  /**
   * 增加 一个 选中 拖拽对象
   * @param {string} newVal
   */
  set dragData(newVal) {
    let curDragListData = this.getAttribute(DL_body_attribute.data);
    let str;

    if (['', undefined].includes(curDragListData)) {
      str = newVal;
      this.dragLisT_floatDomEl.firstChild.innerHTML = 1;
    } else {
      if (curDragListData == newVal) {
        str = '';
        this.dragLisT_floatDomEl.firstChild.innerHTML = 0;
      } else {
        let curDragListDataValArr = curDragListData.split(split_dl_data);

        /**
         * @type {[]}
         */
        if (curDragListDataValArr.includes(newVal)) {
          removeObjectOnlyPushSet(curDragListDataValArr, newVal);
        } else {
          curDragListDataValArr.push(newVal);
        }
        str = curDragListDataValArr.join(split_dl_data);
        this.dragLisT_floatDomEl.firstChild.innerHTML =
          curDragListDataValArr.length;
      }
    }
    this.setAttribute(DL_body_attribute.data, str);
  }

  /**
   * @returns {string[]}
   */
  get dragData() {
    return this.getAttribute(DL_body_attribute.data).split(split_dl_data);
  }

  /**
   * 设置 to el 的 key 值
   * @param {string} newVal
   */
  set dragToEl(newVal) {
    this.setAttribute(DL_body_attribute.toEl, newVal);
  }

  /**
   * 获取 to el 的 key 值
   */
  get dragToEl() {
    return this.getAttribute(DL_body_attribute.toEl);
  }

  /**
   * 移除 body 上 dragList 所有 属性
   */
  removeBodyAttribute() {
    for (const key in DL_body_attribute) {
      this.removeAttribute(key);
    }
  }

  /**
   * 置空 拖拽列表 dragList
   */
  resetDragList() {
    this.setAttribute(DL_body_attribute.data, '');
    this.dragLisT_floatDomEl.firstChild.innerHTML = 0;
  }

  /**
   * 分析 fromto spacename 结果
   * @returns {{state:DL_body_removeAnalysis}}
   */
  analysisFromTo() {
    let {
      [DL_body_attribute.from]: fromSpace,
      [DL_body_attribute.to]: toSpace,
    } = this.getAllAttribute();

    return new dragList_fromToSapcename_analysis(fromSpace, toSpace);
  }
}

export { dragList_dataMana_body, dl_body_spacename };
